//
//  BiDocListViewController.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15/5/4.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiDocListViewController.h"
#import "BiBillManageViewController.h"
#import "BiDocInfoManageViewController.h"
#import "BiDocInfoUploadViewController.h"

#import "MJRefresh.h"
#import "BiDocListCell.h"

#import "BiCarLiscenceComEntity.h"

@interface BiDocListViewController ()<UITableViewDataSource, UITableViewDelegate>{
    UITableView * _tableView;
    
    NSMutableArray * _docExecuteList;
    
    
}

@end

@implementation BiDocListViewController

- (id)init
{
    self = [super init];
    if (self)
    {
        _docExecuteList = [[NSMutableArray alloc] init];
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBackBtn];
    
    if (2 == self.type) {
        [self setNavBarTitle:@"开缴款单审核"];
        
    }else if (4 == self.type){
        [self setNavBarTitle:@"牌照信息上传"];
        
    }else if (5 == self.type){
        [self setNavBarTitle:@"牌照信息审批"];
        
    }
    
    [self setNavBarWithHidden:NO];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self viewTop], MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - [self viewTop])];
    
    _tableView.delegate = self;
    
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    _tableView.rowHeight = 80;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setupRefresh];
}

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    //    [_tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    _tableView.headerPullToRefreshText = @"下拉刷新";
    _tableView.headerReleaseToRefreshText = @"松开即可刷新";
    _tableView.headerRefreshingText = @"刷新中~";
    
    _tableView.footerPullToRefreshText = @"上拉加载更多数据";
    _tableView.footerReleaseToRefreshText = @"松开马上加载";
    _tableView.footerRefreshingText = @"加载中~";
}

#pragma mark 视图即将刷新
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:[BiUserEntity sharedInstance].userid,@"userId",
                              @"1",@"page",
                              PAGESIZE,@"pageSize",
                              [NSString stringWithFormat:@"%d",(int)self.type],@"status",
                              self.licenseType,@"licenseType",
                              nil];

        
    [[BiNetEngine shareInstance] requestDocmengList:self paraDict:paraDic];
    
    [self asyshowHUDView:WAITING CurrentView:self];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing{
    
    NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:[BiUserEntity sharedInstance].userid,@"userId",
                              @"1",@"page",
                              PAGESIZE,@"pageSize",
                              [NSString stringWithFormat:@"%d",(int)self.type],@"status",
                              self.licenseType,@"licenseType",
                              nil];
    
    [[BiNetEngine shareInstance] requestDocmengList:self paraDict:paraDic];

}

- (void)fffff{
    
    [_tableView headerEndRefreshing];
    [_tableView footerEndRefreshing];
    
}

- (void)footerRereshing{
    
    [self performSelector:@selector(fffff) withObject:nil afterDelay:2];
    NSLog(@"footerRereshing");
}

#pragma -mark UitabeViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (self.type) {
            
        case 2:{
            
            BiBillManageViewController * cVtrcl = [[BiBillManageViewController alloc] init];
            cVtrcl.cEntity = _docExecuteList[indexPath.row];
            [self.navigationController pushViewController:cVtrcl animated:YES];
            
        }
            break;
        case 4:{
            
            BiDocInfoUploadViewController * cVtrcl = [[BiDocInfoUploadViewController alloc] init];
            cVtrcl.cEntity = _docExecuteList[indexPath.row];
            [self.navigationController pushViewController:cVtrcl animated:YES];
            
        }
            break;
        case 5:{
            
            BiDocInfoManageViewController * cVtrcl = [[BiDocInfoManageViewController alloc] init];
            cVtrcl.cEntity = _docExecuteList[indexPath.row];
            [self.navigationController pushViewController:cVtrcl animated:YES];
            
        }
            break;
            
        default:
            break;
    }

}


#pragma -mark UitabeViewDatasource

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_docExecuteList) {
        
        return _docExecuteList.count;
    }
    
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *myIndentifier = @"BiDocListCell";
    
    BiDocListCell *cell = (BiDocListCell *)[_tableView dequeueReusableCellWithIdentifier:myIndentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BiDocListCell" owner:self options:nil]firstObject];
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    }
//    _docExecuteList把每行的值给 cEntity
    BiCarLiscenceComEntity *cEntity = _docExecuteList[indexPath.row];
//    数据填充到cell
//    NSLog(@"%ld",indexPath.row);
    [cell carliscenceDAtaFill:cEntity];
    
    return cell;
}


#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    [self showMessage:entity_[@"result_info"]];
    
    if ([entity_[@"response_status"] integerValue] != 200) {
        
        return;
        
    }
    [_tableView headerEndRefreshing];
    [_tableView footerEndRefreshing];
    
    [_docExecuteList removeAllObjects];
//    把值给_docExecuteList
    [_docExecuteList addObjectsFromArray:entity_[@"data"]];
    
    [_tableView reloadData];
    
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    [self removeHUDView:self];
}


@end
